module IRC
  class Response
    class << self
      def parse(line)
        matches = line.chomp.match(/^(:([^ ]+))?(.+?)( :(.*))?$/)
        from = matches[2]
        name, *args = matches[3].strip.split
        text = matches[5]

        new(from, name.upcase, args, text)
      end
    end
    
    attr_reader :from, :name, :args, :text

    def initialize(from, name, args, text)
      @from, @name, @args, @text = from, name, args, text
    end
    
    def inspect
      result = name.dup
      result << ":#{from}" unless from.empty? if from
      result << "(#{args.join(", ")})" unless args.empty?
      result << " #{text}"
    end
  end
end
